package weaver.interfaces.qz.job;

import cn.hutool.core.date.DateUtil;
import com.engine.qz.util.QzUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.ThreadPoolUtil;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2025/5/19
 * @Description 所有部门的一级部门
 * @Author Lee
 */
public class FirstDeptJob extends BaseCronJob {

    BaseBean baseBean = new BaseBean();

    @Override
    public void execute() {
        try {
            RecordSet rs = new RecordSet();
            String sql = "select deptid from uf_first_dept";
            rs.executeQuery(sql);
            HashSet<String> set = new HashSet<>();
            while (rs.next()) {
                set.add(rs.getString("deptid"));
            }
            sql = "WITH DepartmentHierarchy AS (\n" +
                    "    SELECT \n" +
                    "        id, \n" +
                    "        supdepid, \n" +
                    "        id AS root_depid\n" +
                    "    FROM \n" +
                    "        HrmDepartment\n" +
                    "    WHERE \n" +
                    "        supdepid = 0\n" +
                    "    UNION ALL\n" +
                    "    SELECT \n" +
                    "        d.id, \n" +
                    "        d.supdepid,\n" +
                    "        CASE WHEN h.supdepid = 0 THEN h.id ELSE h.root_depid END AS root_depid\n" +
                    "    FROM \n" +
                    "        HrmDepartment d\n" +
                    "    INNER JOIN \n" +
                    "        DepartmentHierarchy h ON d.supdepid = h.id\n" +
                    ")\n" +
                    "SELECT \n" +
                    "    dh.id AS deptid,\n" +
                    "    dh.supdepid AS pid,\n" +
                    "    dh.root_depid AS tid,\n" +
                    "    td.departmentname AS tname\n" +
                    "FROM \n" +
                    "    DepartmentHierarchy dh\n" +
                    "LEFT JOIN \n" +
                    "    HrmDepartment td ON dh.root_depid = td.id\n" +
                    "ORDER BY \n" +
                    "     dh.id";
            rs.executeQuery(sql);
            List<List> updateList = new ArrayList<>();
            List<List> inserteList = new ArrayList<>();
            while (rs.next()) {
                String deptid = rs.getString("deptid");
                String pid = rs.getString("pid");
                String tid = rs.getString("tid");
                String tname = rs.getString("tname");
                if (set.contains(deptid)) {
                    ArrayList<String> list = new ArrayList<>();
                    list.add(pid);
                    list.add(tid);
                    list.add(tname);
                    list.add(deptid);
                    updateList.add(list);
                } else {
                    ArrayList<String> list = new ArrayList<>();
                    list.add(deptid);
                    list.add(pid);
                    list.add(tid);
                    list.add(tname);
                    inserteList.add(list);
                }
            }
            String insertSql = "insert into uf_first_dept (deptid,pid,tid,tname) values(?,?,?,?)";
            String updateSql = "update uf_first_dept set pid=?,tid=?,tname=? where deptid=?";
            rs.executeBatchSql(insertSql, inserteList);
            rs.executeBatchSql(updateSql, updateList);
        } catch (Exception e) {
            baseBean.writeLog("FirstDeptJob error:" + e.getMessage());
        }
    }
}
